package com.match.competition_apply.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.match.competition_apply.entity.CompetitionInfo;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CompetitionExcelByHutoolUtils {
    public static void downloadCompetitionExcel(HttpServletResponse response, String fileName, List<CompetitionInfo> competitionInfoList){
        List<CompetitionInfo> rows = CollUtil.newArrayList();
        for (CompetitionInfo competitionInfo : competitionInfoList) {
            competitionInfo.setBegintime(parseTimeToString(competitionInfo.getCompetitionBegin()));
            competitionInfo.setEndtime(parseTimeToString(competitionInfo.getCompetitionEnd()));
            rows.add(competitionInfo);
        }
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter();

        //自定义标题别名
        writer.addHeaderAlias("id", "竞赛编号");
        writer.addHeaderAlias("competitionName", "竞赛名称");
        writer.addHeaderAlias("competitionOrganizer", "竞赛组织员");
        writer.addHeaderAlias("competitionLever", "竞赛等级");
        writer.addHeaderAlias("entryLever", "参赛等级");
        writer.addHeaderAlias("competitionType", "竞赛类型");
        writer.addHeaderAlias("directoryInOut", "目录内外");
        writer.addHeaderAlias("newAdd", "是新增");
        writer.addHeaderAlias("rewardsBureau", "颁奖单位");
        writer.addHeaderAlias("competitionDepartment", "竞赛组织部门");
        writer.addHeaderAlias("replaceCredits", "可替换学分");
        writer.addHeaderAlias("replaceCourse", "可替换学分类型");
        writer.addHeaderAlias("examDate", "考试时间");
        writer.addHeaderAlias("queueCount", "队伍数");
        writer.addHeaderAlias("queuePersonCount", "队伍人数");
        writer.addHeaderAlias("Begintime", "比赛开始时间");
        writer.addHeaderAlias("Endtime", "比赛结束时间");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(15, "大创申请项目");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        // 设置所有列为自动宽度，不考虑合并单元格
        writer.autoSizeColumnAll();
        //设置内容字体
        Font font = writer.createFont();
        font.setBold(false);
        font.setItalic(false);
        StyleSet style = writer.getStyleSet();
        //第二个参数表示是否忽略头部样式
        style.setFont(font, true);
        writer.setSheet(fileName);

        response.reset();//清空缓存区，防止存在某些字符使得下载的文件格式错误
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            response.setHeader("Content-Disposition","filename="+java.net.URLEncoder.encode(fileName,"utf-8")+".xlsx");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ServletOutputStream out= null;
        try {
            out = response.getOutputStream();
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        // 关闭writer，释放内存
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    /**
     * 时间(yyyy-MM-dd HH:mm:ss):Date格式转String格式
     */
    public static String parseTimeToString(Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
        return sdf.format(date);
    }
}
